<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="../js/jquery.js"></script>
        <script src="../js/axios.min.js"></script>
        <script src="../js/jquery.validate.min.js"></script>
        <script src="../js/layer.min.js"></script>
    </head>
    <body>
        <div id="box">
            <div id="left">
                <img src alt>
            </div>
            <div id="right">
                <p>
                    名称：<span></span>
                </p>
                <p>
                    价格：<span></span>
                </p>
                <p>
                    <button id="sub">-</button>
                    <input id="num" style="width: 30px;" type="number"
                        value="1">
                    <button id="increment">+</button>
                    <button id="addCart">加入购物车</button>
                </p>
            </div>
        </div>
        <script>
            axios.defaults.baseURL = 'http://60.204.218.206:8088/';

            //请求拦截
            axios.interceptors.request.use(function(config){
                // console.log(config);
                config.headers.token = localStorage.getItem('token');
                return config;
            })



            let id=1
            axios({
                url: '/goods/goodsById?id='+id                
            }).then(function(res){
                console.log(res);

                $('#left img').attr('src',"http://60.204.218.206:8088/"+res.data.img);
                // $('#right p:first span').text(res.data.pname);
                // 或写成：
                $('#right span:eq(0)').text(res.data.pname);
                $('#right span:eq(1)').text(res.data.price);   
                //往加入购物车的按钮上藏了一个对象，就是当前产品相关信息) 
                $('#addCart').data("info",res.data)            
            })

            $(function(){
                $("#right p #sub").on("click",function(){
                    if($(this).next().val()==1){
                        return;
                    }
                    $(this).next().val(parseInt($(this).next().val())-1);
                })

                $("#right p #increment").on("click",function(){                    
                    $(this).prev().val(parseInt($(this).prev().val())+1);
                })

                $("#right #addCart").on("click",function(){
                    // 购买人的id
                    let uId=JSON.parse(localStorage.getItem("user")||'{}').u_id
                    // console.log(1,uId);
                    // 商品 id
                    let pId=$("#addCart").data("info").id
                    // console.log(2,pId);
                    let pName=$("#addCart").data("info").pname
                    // console.log(3,pName);
                    let pPrice=$("#addCart").data("info").price
                    // console.log(4,pPrice);
                    let pImg=$("#addCart").data("info").img
                    // console.log(5,pImg);
                    let pNum=$("#right p #num").val()
                    // console.log(6,pNum);


                    // request.Info.u_id,
                    // request.body.pid,
                    // request.body.pname,
                    // request.body.pimg,
                    // request.body.pnumber,
                    // request.body.pprice,

                    let data=new URLSearchParams();
                    // data.append("u_id",uId)
                    data.append("pid",pId)
                    data.append("pname",pName) 
                    data.append("pimg",pImg)
                    data.append("pnumber",pNum)
                    data.append("pprice",pPrice)
                    console.log(data);

                    axios({
                        method:"post",
                        url:"/cart/add",
                        data:data                    
                    }).then(function(res){
                         layer.msg(res.data.msg)
                        })
                    })
            })
    
        </script>
    </body>
</html>